package com.geek.water.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/8/3
 */
public class NumberToChinese {
    private static final String[] CN_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CN_UNITS = {"", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};
    private static final String CN_DECIMAL_UNIT = "角分";
    private static final String CN_FULL = "整";
    private static final String CN_NEGATIVE = "负";
    private static final String CN_YUAN = "元";

    public static String convert(String amount) {
        if (amount == null || amount.isEmpty()) {
            return "";
        }

        try {
            BigDecimal number = new BigDecimal(amount).setScale(2, RoundingMode.HALF_UP);
            if (number.compareTo(BigDecimal.ZERO) == 0) {
                return "零元整";
            }

            boolean isNegative = number.compareTo(BigDecimal.ZERO) < 0;
            if (isNegative) {
                number = number.abs();
            }

            String numStr = number.toString();
            String[] parts = numStr.split("\\.");
            String integerPart = parts[0];
            String decimalPart = parts.length > 1 ? parts[1] : "00";

            if (decimalPart.length() == 1) {
                decimalPart += "0";
            }

            StringBuilder result = new StringBuilder();
            if (isNegative) {
                result.append(CN_NEGATIVE);
            }

            // 处理整数部分
            if (!integerPart.equals("0")) {
                result.append(convertIntegerPart(integerPart)).append(CN_YUAN);
            }

            // 处理小数部分
            if (!decimalPart.equals("00")) {
                result.append(convertDecimalPart(decimalPart));
            } else {
                result.append(CN_FULL);
            }

            return result.toString();
        } catch (Exception e) {
            return "金额转换错误";
        }
    }

    private static String convertIntegerPart(String integerPart) {
        StringBuilder result = new StringBuilder();
        int len = integerPart.length();
        boolean lastZero = false;

        for (int i = 0; i < len; i++) {
            int digit = integerPart.charAt(i) - '0';
            int unitIndex = len - i - 1;
            int unit = unitIndex % 4;
            int section = unitIndex / 4;

            if (digit == 0) {
                if (!lastZero && (unit == 0 || section > 0)) {
                    result.append(CN_NUMBERS[digit]);
                    lastZero = true;
                }
            } else {
                if (lastZero) {
                    result.append(CN_NUMBERS[0]);
                }
                result.append(CN_NUMBERS[digit]).append(CN_UNITS[unit]);
                lastZero = false;
            }

            // 处理万/亿单位
            if (unit == 0 && section > 0) {
                result.append(CN_UNITS[4 + section]);
                lastZero = false;
            }
        }

        return result.toString();
    }

    private static String convertDecimalPart(String decimalPart) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 2; i++) {
            int digit = decimalPart.charAt(i) - '0';
            if (digit != 0) {
                result.append(CN_NUMBERS[digit]).append(CN_DECIMAL_UNIT.charAt(i));
            }
        }
        return result.toString();
    }
}